<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力中小企业发展 
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
+----------------------------------------------------------------------
| 添加/编辑紧急联系人 开发者: 麦沃德科技-半夏
+---------------------------------------------------------------------- -->

<template>
	<view class="container" :style="{'--theme-color': themeColor}" v-if="loadEnd">
		<!-- 表单区 -->
		<view class="container-form">
			<view class="form-item">
				<input class="item-input" v-model="formData.mobile" type="number" maxlength="11" placeholder="请输入紧急联系人手机号码" placeholder-class="item-placeholder" />
			</view>
			<view class="form-item">
				<input class="item-input" v-model="formData.name" type="text" placeholder="紧急联系人姓名(选填)" />
			</view>
			<view class="form-item">
				<view class="item-default flex flex-between-center">
					<view class="default-label">优先联系人</view>
					<view class="default-switch" :class="{select: formData.set_priority == 1}" @click="formData.set_priority = formData.set_priority == 1 ? 2 : 1">
						<view class="round"></view>
					</view>
				</view>
				<view class="item-tips">如遇紧急情况平台无法联系到您时，会给TA打电话</view>
			</view>
		</view>
		<!-- 提示 -->
		<view class="container-tips">添加或修改紧急联系人后请及时告知对方</view>
		<!-- 底部按钮 -->
		<view class="container-footer">
			<view class="footer-btn" @click="handleSubmit()">确认{{passengerId ? "编辑" : "添加"}}</view>
			<view class="safe-padding"></view>
		</view>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	export default {
		data() {
			return {
				// 加载完成
				loadEnd: false,
				// 紧急联系人id
				passengerId: null,
				// 表单内容
				formData: {
					mobile: "",
					name: "",
					set_priority: 2,
				},
				// 延时器
				timeout: null,
			}
		},
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
			})
		},
		onLoad(option) {
			if (option && option.id) {
				this.passengerId = option.id
				this.$nextTick(() => {
					uni.setNavigationBarTitle({
						title: "编辑"
					})
				})
				uni.showLoading({
					title: "加载中"
				})
				this.getPassengerDetails(() => {
					uni.hideLoading()
					this.loadEnd = true
				})
			} else {
				this.$nextTick(() => {
					uni.setNavigationBarTitle({
						title: "添加"
					})
				})
				this.loadEnd = true
			}
		},
		onUnload() {
			clearTimeout(this.timeout)
		},
		methods: {
			// 获取紧急联系人详情
			getPassengerDetails(fn) {
				this.$util.request("passenger.contacts.details", {
					id: this.passengerId
				}).then(res => {
					if (fn) fn()
					if (res.code == 1) {
						this.formData = res.data
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					if (fn) fn()
					console.error('获取紧急联系人详情 ', error)
				})
			},
			// 添加/编辑
			handleSubmit() {
				if (!this.formData.mobile) {
					uni.showToast({
						icon: "none",
						title: "请输入紧急联系人手机号码",
					})
					return
				}
				if (!this.$util.validation("phone", this.formData.mobile)) {
					uni.showToast({
						icon: "none",
						title: "请输入正确的手机号码"
					})
					return
				}
				uni.showLoading({
					title: "加载中",
					mask: true
				})
				var url = ""
				if (this.passengerId) url = "passenger.contacts.edit"
				else url = "passenger.contacts.add"
				this.$util.request(url, this.formData).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						if (this.formData.id) {
							uni.showToast({
								icon: "success",
								title: "编辑成功",
								mask: true,
								duration: 1500
							})
						} else {
							uni.showToast({
								icon: "success",
								title: "添加成功",
								mask: true,
								duration: 1500
							})
						}
						this.timeout = setTimeout(() => {
							uni.navigateBack()
						}, 1500);
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					uni.hideLoading()
					console.error('添加/编辑紧急联系人 ', error)
				})
			},
		}
	}
</script>

<style lang="scss">
	.container {
		padding: 44rpx 32rpx 144rpx;

		.container-form {
			.form-item {
				margin-top: 32rpx;
				border-radius: 32rpx;
				background: #FFF;
				overflow: hidden;

				&:first-child {
					margin-top: 0;
				}

				.item-input {
					color: #242629;
					font-size: 32rpx;
					font-weight: 600;
					line-height: 44rpx;
					padding: 42rpx 48rpx;
				}

				.item-placeholder {
					color: #242629;
				}

				.item-default {
					padding: 32rpx 48rpx 0;
					border-radius: 32rpx;
					background: #FFF;

					.default-label {
						color: #242629;
						font-size: 28rpx;
						font-weight: 600;
						line-height: 40rpx;
					}

					.default-switch {
						width: 80rpx;
						height: 40rpx;
						padding: 3rpx;
						background: #EBEBEB;
						border-radius: 20rpx;
						transition: all .3s;

						.round {
							width: 34rpx;
							height: 34rpx;
							border-radius: 50%;
							background: #fff;
							margin-left: 0;
							transition: all .3s;
						}

						&.select {
							background: var(--theme-color);

							.round {
								margin-left: calc(100% - 34rpx);
							}
						}
					}
				}

				.item-tips {
					color: #8D929C;
					font-size: 24rpx;
					font-weight: 600;
					line-height: 34rpx;
					margin-top: 16rpx;
					padding: 0 48rpx 32rpx;
				}
			}
		}

		.container-tips {
			color: #8D929C;
			font-size: 24rpx;
			font-weight: 600;
			line-height: 34rpx;
			margin-top: 32rpx;
		}

		.container-footer {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
			z-index: 96;
			background: #ffffff;
			border-top: 1rpx solid #F6F7FB;
			padding: 16rpx 24rpx;

			.footer-btn {
				color: #ffffff;
				font-size: 28rpx;
				line-height: 40rpx;
				padding: 20rpx 24rpx;
				border-radius: 40rpx;
				background: var(--theme-color);
				text-align: center;
			}
		}
	}
</style>